﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace FFWinMobile.controls
{
    public partial class ucSearch : UserControl
    {
        public ucSearch()
        {
            InitializeComponent();
        }

        public string SearchText
        {
            get
            {
                return txtSearch.Text;
            }
            set
            {
                txtSearch.Text = value;
            }
        }

        string msg = string.Empty;
        private void btnSearch_Click_1(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                frmSearch frm = new frmSearch(txtSearch.Text);
                frm.Show();
            }
        }
        List<SearchResponse> searchList;
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;

            if (!string.IsNullOrEmpty(t.Text) & t.Text.Length >= 2)
            {
                Cursor.Current = Cursors.WaitCursor;
                if (string.IsNullOrEmpty(msg))
                {
                   
                    msg = FF.FeedInfo("me");
                    TransferToList(msg);
                }

                Panel pnlSuggestBox;               
                TextBox txt;
                ListBox lb = null;
                LinkLabel lblClose;
                RadioButton cbFriends, cbAll;
                if (this.Parent.Controls[0] as Panel == null)
                {
                    pnlSuggestBox = new Panel();
                    pnlSuggestBox.Height = 200;
                    pnlSuggestBox.Width = 200;
                    pnlSuggestBox.BackColor = Color.SlateGray;

                    Panel pnlFeeds = (Panel)this.Parent.Controls[1];
                    pnlSuggestBox.Location = new Point(15, 60);
                    pnlFeeds.Enabled = false;

                    this.Parent.Controls.Add(pnlSuggestBox);
                    pnlSuggestBox.BringToFront();
                    pnlFeeds.Location = new Point(3, 3);
                    cbAll = new RadioButton();
                    cbFriends = new RadioButton();
                    txt = new TextBox();
                    lb = new ListBox();

                    lblClose = new LinkLabel();
                    lblClose.Text = "X";
                    lblClose.Width = 20;
                    lblClose.Location = new Point(182, 3);
                    lblClose.Size = new Size(10, 15);
                    lblClose.Click += new EventHandler(lblClose_Click);
                    lblClose.Font = new Font(FontFamily.GenericSansSerif, 12f, FontStyle.Bold);
                    lblClose.ForeColor = Color.Black;
                    pnlSuggestBox.Enabled = true;
                    pnlSuggestBox.Controls.Add(lblClose);

                    lb.Height = 140;
                    lb.Width = 190;
                    lb.Location = new Point(5, 27);
                    
                    cbFriends.Location = new Point(5, 155);
                    cbFriends.Width = 190;
                    cbAll.Width = 190;
                    cbFriends.Tag = "friends";
                    cbAll.Tag = "";
                    cbAll.Location = new Point(5, 175);
                    cbFriends.Text = Properties.Resources.SEARCH_FRIENDS;
                    cbAll.Text = Properties.Resources.SEARCH_EVERYWHERE;
                    pnlSuggestBox.Controls.Add(cbFriends);
                    pnlSuggestBox.Controls.Add(cbAll);
                    cbFriends.Click += new EventHandler(cbFriends_Click);
                    cbAll.Click += new EventHandler(cbFriends_Click);
                   
                }
                else
                {
                    pnlSuggestBox = (Panel)this.Parent.Controls[0];
                    txt = (TextBox)pnlSuggestBox.Controls[3];
                    lb = (ListBox)pnlSuggestBox.Controls[4];
                    cbFriends = (RadioButton)pnlSuggestBox.Controls[1];
                    cbAll = (RadioButton)pnlSuggestBox.Controls[2];
                }
                    
                    txt.Width = 175;
                    txt.Location = new Point(5, 3);
                    txt.Text = t.Text;
                    pnlSuggestBox.Controls.Add(txt);
                    txt.TextChanged -= new EventHandler(txtSearch_TextChanged);
                    txt.TextChanged += new EventHandler(txtSearch_TextChanged);

                    List<SearchResponse> arama = searchList.FindAll(delegate(SearchResponse sr)
                    {
                        return sr.UserName.ToLower().Contains(t.Text.ToLower());
                    });
                    lb.SelectedIndexChanged -= new EventHandler(lb_SelectedIndexChanged);                 
                    lb.DataSource = arama;
                    lb.DisplayMember = "UserName";
                    lb.ValueMember = "id";
                    lb.BackColor = Color.SlateGray;
                    lb.ForeColor = Color.WhiteSmoke;
                    pnlSuggestBox.Controls.Add(lb);                    
                    lb.SelectedIndexChanged += new EventHandler(lb_SelectedIndexChanged);
                    Cursor.Current = Cursors.Default;
            }
        }

        void cbFriends_Click(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Tag.ToString() == "friends")
            {
                Panel pnlSuggestBox = (Panel)this.Parent.Controls[0];
                TextBox txt = (TextBox)pnlSuggestBox.Controls[3];
                frmSearch frm = new frmSearch(txt.Text, "friends", GF.UserName);
                lblClose_Click(null, null);
                frm.Show();
            }
            else
            {
                Panel pnlSuggestBox = (Panel)this.Parent.Controls[0];
                TextBox txt = (TextBox)pnlSuggestBox.Controls[3];
                frmSearch frm = new frmSearch(txt.Text);
                lblClose_Click(null, null);
                frm.Show();
            }
        }

        void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lb = (ListBox)sender;
            if (lb.SelectedValue != null)
            {
                SearchResponse selected = searchList.Find(delegate(SearchResponse sr) { return sr.Id.Equals(lb.SelectedValue.ToString()); });
                frmUserForm frm = new frmUserForm(selected.Id);
                lblClose_Click(null, null);
                frm.Show();
            }
           
        }

        private void TransferToList(string msg)
        {
            JObject j = JObject.Parse(msg);
            searchList = new List<SearchResponse>();
            foreach (JToken s in j["subscriptions"])
            {

                searchList.Add(new SearchResponse() { Id = s["id"].Value<string>(), UserName = s["name"].Value<string>(), Type = s["type"].Value<string>() });
            }
        }

        void lblClose_Click(object sender, EventArgs e)
        {
            Panel pnlSug = (Panel)this.Parent.Controls[0];
            Panel pnlFeeds = (Panel)this.Parent.Controls[2];
            this.Parent.Controls.RemoveAt(0);
            pnlFeeds.Enabled = true;
        }

    
    }
    public class SearchResponse
    {
        public string UserName { get; set; }
        public string Id { get; set; }
        public string Type { get; set; }
    }
}
